import { request } from "../../utlis/asyncWx";
// import regeneratorRuntime from "../../lib/runtime/runtime";
// pages/goods_list/index.js
Page({
  data: {
    tabs: [{
      id: 0,
      value: "综合",
      isActive: true
    },
    {
      id: 1,
      value: "销量",
      isActive: false
    },
    {
      id: 2,
      value: "价格",
      isActive: false
    }
    ],
    // 请求数据
    queryInfo: {
      query: '',
      cid: '',
      pagenum: 1,
      pagesize: 10
    },
    goodsList: [],
    totalPage: 0
  },
  // 接收子组件传值
  onTabsItemChange(e) {
    // console.log(e.detail)
    const {
      index
    } = e.detail
    // 获取以前的tabs
    let {
      tabs
    } = this.data
    // 组个操作
    tabs.forEach((v, i) => {
      i === index ? v.isActive = true : v.isActive = false
    })
    // 更新以前数据
    this.setData({
      tabs
    })

  },
  // 请求
  async getGoodsList() {
    const {
      data: res
    } = await request({
      url: "goods/search",
      data: this.data.queryInfo
    })
    console.log('请求列表', res)
    this.setData({
      goodsList: [...this.data.goodsList, ...res.message.goods],
      totalPage: Math.ceil(res.message.total / this.data.queryInfo.pagesize)
    })
    console.log('页数totalPage', this.data.totalPage)
    console.log('goodsList', this.data.goodsList)
    wx.stopPullDownRefresh();
  },
  onLoad: function (options) {
    // 通过 分类页面 传入的cid 有则用没有就空
    this.data.queryInfo.cid = options.cid || ""
    this.data.queryInfo.query = options.query || ""
    // 调用请求
    this.getGoodsList();
  },
  // 页面触底
  onReachBottom: function () {
    // console.log('触底')
    if (this.data.queryInfo.pagenum >= this.data.totalPage) {
      wx.showToast({
        title: '没有下一页数据了'
      });
      console.log('%c没数据了', 'color: #43bb88;font-size: 24px;font-weight: bold;text-decoration: underline;');
      this.data.queryInfo.pagenum = this.data.totalPage
    } else {
      this.data.queryInfo.pagenum++
      console.log(this.data.queryInfo.pagenum)
      // 获取数据列表
      this.getGoodsList();
    }
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    console.log('refresh')
    this.data.goodsList = [],
      this.getGoodsList();
  },

})